package com.jie.component.exception.handler;

import com.jie.common.util.LogUtil;
import com.jie.component.exception.base.AuthenticationException;
import com.jie.component.exception.base.BusinessException;
import com.jie.component.exception.base.FrameException;
import com.jie.component.exception.base.ValidationException;
import com.jie.common.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author jie
 * @version 1.0 2025-02-10
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = FrameException.class)
    public Result<?> frameExceptionHandler(FrameException e) {
        LogUtil.frameExceptionLog(e.getMsg());
        return Result.error(e.getMsg(), e.getCode());
    }

    @ExceptionHandler(value = BusinessException.class)
    public Result<?> businessExceptionHandler(BusinessException e) {
        LogUtil.businessExceptionLog(e.getMsg());
        return Result.error(e.getMsg(), e.getCode());
    }

    @ExceptionHandler(value = ValidationException.class)
    public Result<?> validationExceptionHandler(ValidationException e) {
        LogUtil.validationExceptionLog(e.getMsg());
        String msg = String.join("、", e.getMsg());
        return Result.error(msg, e.getCode());
    }

    @ExceptionHandler(value = AuthenticationException.class)
    public Result<?> authenticationExceptionHandler(AuthenticationException e) {
        LogUtil.authenticationExceptionLog(e.getMsg());
        return Result.error(e.getMsg(), e.getCode());
    }
}
